package com.common.properties;

import cn.hutool.crypto.SecureUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

import java.util.List;

@ConfigurationProperties(prefix = "security")
@EnableConfigurationProperties(SecurityProperties.class)
@Data
@Slf4j
public class SecurityProperties implements InitializingBean {

    private List<String> exclude_url;
    private SecurityPropertiesAuthentication authentication;

    /**
     * 由于JWT 0.12.3的版本中 对密钥的安全性提高了 不可直接使用简易密钥
     * 不可使用强散列加密  BCryptPasswordEncoder 会导致每一次加密的结果都不一样，间接导致每个微服务得到的密钥都不一样
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        log.debug("Properties 'SecurityProperties' configured for use");
        // 使用MD5加密算法 对简易密钥进行二次加密
        String accessSecretKey = this.authentication.access_secretKey;  // 正确认证密钥
        String refresh_secretKey = this.authentication.refresh_secretKey;  // 正确刷新密钥
        this.authentication.access_secretKey = SecureUtil.md5(accessSecretKey); // 进行MD5加密
        this.authentication.refresh_secretKey = SecureUtil.md5(refresh_secretKey); // 进行MD5加密
    }

    @Data
    public static class SecurityPropertiesAuthentication {
        private String access_secretKey;
        private String access_name;
        private Integer access_expireHours;
        private String refresh_secretKey;
        private String refresh_name;
        private Integer refresh_expireHours;
    }

}


